package com.example.lib5.linkedList;


public class Josephu {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy2(5);
        circleSingleLinkedList.showBoy();
        circleSingleLinkedList.countBoy(1, 2, 5);
    }

    private static class CircleSingleLinkedList {
        private Boy first = null;

        public void addBoy(int nums) {
            //节点小于等于于1的话，直接跳出去
            if (nums < 1) {
                System.out.println("不能为0或者小于0");
                return;
            }
            Boy curBoy = null;
            for (int i = 1; i < nums + 1; i++) {
                //根据编号创建小孩节点
                Boy boy = new Boy(i);
                //如果是第一个孩子
                if (first == null) {
                    first = boy;
                    first.setNext(first);
                    curBoy = first;
                } else {
                    curBoy.setNext(boy);
                    boy.setNext(first);
                    curBoy = boy;
                }
            }

        }
        public void addBoy2(int nums){
            //判断是否合理 合里遍历  是否为null节点
            if (nums<=0) {System.out.println("输入的值有误"); return;}
            Boy curBoy=null;
            for (int i = 1; i < nums+1; i++) {
                Boy boyB = new Boy(i);
                if (first==null){
                    first=boyB;
                    boyB.setNext(first);
                    curBoy=boyB;
                }else {
                    curBoy.setNext(boyB);
                    boyB.setNext(first);
                    curBoy=boyB;

                }
            }
        }
        public void showBoy() {
            //判断链表是否为空
            if (first == null) {
                System.out.println("链表为空");
                return;
            }
            Boy curBoy = first;
            while (true) {
                //打印当前链表
                System.out.printf("小孩的编号 %d\n", curBoy.getNo());
                //判断是否为最后一个链表
                if (curBoy.getNext() == first) {
                    break;
                }
                //后移
                curBoy = curBoy.getNext();
            }
        }

        public void countBoy(int starNo, int countNum, int nums) {
            //去掉不合理
            if (first == null || starNo < 1 || starNo > nums) {
                System.out.println("参数输入有误，请重新输入");
                return;
            }
            //创建帮助指针
            Boy helper = first;
            while (true) {
                if (helper.getNext() == first) {
                    break;
                }
                helper = helper.getNext();
            }
            //小孩报数前，先让first和helper移动k-1次
            for (int j = 0; j < starNo - 1; j++) {
                first=first.getNext();
                helper=helper.getNext();
            }
            //当小孩报数时，让first和helper指针同时的移动m-1次，然后出圈
            while (true) {
                if (helper == first) {
                    break;
                }
                //让first和helper指针同时移动countNum-1
                for (int i = 0; i < countNum - 1; i++) {
                    first = first.getNext();
                    helper = helper.getNext();
                }
                //这是first指向的节点就是要出圈的节点
                System.out.println("小孩%出圈\n"+first.getNo());
                //这时将first指向出圈节点的下一个
                first = first.getNext();
                //设置帮助节点等于first
                helper.setNext(first);
            }
            System.out.println(first.getNo());
        }
    }

    static class Boy {
        private int no;
        private Boy next;

        public Boy(int no) {
            this.no = no;
        }

        public int getNo() {
            return no;
        }

        public void setNo(int no) {
            this.no = no;
        }

        public Boy getNext() {
            return next;
        }

        public void setNext(Boy next) {
            this.next = next;
        }
    }
}
